<?php
/**
 * 本页面用于单个采集
 * 仅供测试用 
 */
	include_once('../sys/sys.php');
	header('Content-Type: text/html; charset=utf-8');
	$url = $_POST['url'];
	if (is_null($url)) {
		exit;
	}
	//分离URL以及ID
	$id = explode("/",$url);
	if ($id[count($id) - 1] == "") {
		$id = $id[count($id) - 2];
	}
	else 
		$id = $id[count($id)-1];
		
	
	$url = 'http://www.89890.com/comic/' . $tid . '/';
	
	//分离URL以及ID
	$id = $tid;
	//定义公用变量
	if (substr($url,-1)!="/")
		$url.="/";
	$url_pre = $url;
	$pic_url_pre = 'http://www.89890.com/comic/';
		
		
	//获取URL内容
//	$url = "1.htm";
	$content = 	file_get_contents($url);
	$content = 	mb_convert_encoding(file_get_contents($url),"UTF-8","GB2312");
	
	//获取名称
	//关键字 漫画_89890漫画网
	$name = explode('漫画_89890漫画网',$content);
	$name = $name[0];
	$name = explode('<title>',$name);
	$name = $name[1];
	
	//获取最后更新时间
	$dt = explode('更新：',$content);
	$dt = $dt[1];
	$dt = explode(' 备注：',$dt);
	$dt = strtotime(trim($dt[0]));
	
	//获取URL列表
	$urls = explode('<table width="100%" border="0" cellpadding="3" cellspacing="1"><tr>',$content);
	$urls = $urls[1];
	$urls = explode('</TD></table>',$urls);
	$urls = $urls[0];
	$replacer = array('<TD width="20%" height=22 vAlign=middle>',' </TD>','target=_blank ','·');
	$replacerc = array('');
	$urls = str_replace($replacer,$replacerc,$urls);
	
	
	$match_str = "/<a href = \'(.*)\'>/isU";
	preg_match_all($match_str,$urls,$urln);
	$urln = $urln[1];
	
	//由于是逆序，需要倒装数组(链接以及每个链接的数量)
	$urln = array_reverse($urln);
	
	
	//插入分类信息
	//检查当前ID是否存在
	$where = 'AND id= ' . $id .'';
	$result = $DB->build_and_exec_query(
		array('select' => 'COUNT(*) as max','from'   => '89890tags','where'	=> "1 " . $where ,)
	);	
	//当已经存在的时候更新日期
	if ($result['max'] > 0) {
		
	}
	//当不存在时插入
	else{
		do_taginsert($id,$dt,$name,0);
	}
	//获取数据库COUNT
	$where = 'AND id= ' . $id .'';
		$result = $DB->build_and_exec_query(
			array('select' => 'count','from'   => '89890tags','where'	=> "1 " . $where ,)
		);	
	$dbcount = $result['count'];
	
	//构造采集相关内容
	//图片服务器数组
	$urlnc = array();
	//每集相关信息解析
	foreach ($urln as $k => $v) {
		//构造JS路径
		$contentc = get_js_url($tid, $v);
		$match_str = "/var total=(.*);var volpic='(.*)';var tpf = (.*);var tpf2 = (.*);/isU";
		preg_match_all($match_str,$contentc,$contentcn);
		$urlnc[$k]['total'] = $contentcn[1][0];
		$urlnc[$k]['volpic'] = $contentcn[2][0];
		$urlnc[$k]['tpf2'] = $contentcn[4][0];
		//获取该集算法编号
		$urlnc[$k]['vol'] = get_pic_cal(file_get_contents('http://www.89890.com/comic/' . $tid . '/' . $v . '/'));
		//入库
		//当COUNT小于已经采集的时候，跳过。
		if ($k < $dbcount) {
			continue;
		}
		do_photoinsert($tid,$urlnc[$k]['volpic'],$urlnc[$k]['total'],$urlnc[$k]['tpf2'],$urlnc[$k]['vol']);
	}
	//更新计数
	$where = 'id= ' . $id .'';
	$DB->do_update('89890tags',
		array(					  
		   'lastupdate'				=> $dt,
		   'count'					=> count($urln),
		 ),
		 $where
	);
	
	
	function do_taginsert($id,$lu,$name,$count){
		global $DB;
		$DB->do_insert( '89890tags', array( 'id'			=> $id,
								   'lastupdate'				=> $lu,
								   'name'					=> $name,
								   'count'					=> $count,
								 )		);
		if ($result === false) {
			show_normal_mesR('无法添加当前漫画','添加出错','添加');
		}
	}
	
	
	function do_photoinsert($uid,$url,$count,$tpf2,$vol){
		global $DB;
		$DB->do_insert( '89890photos', array( 
								   'uid'				=> $uid,
								   'url'					=> $url,
								   'count'					=> $count,
								   'tpf2'					=> $tpf2,
								   'vol'					=> $vol,
								 )		);
		if ($result === false) {
			show_normal_mesR('无法添加当前漫画','添加出错','添加');
		}
	}
	
	echo "采集完毕";
	
	
	exit;
	
	
?>